package ctm.javacode;

import java.io.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.lang.*;
import java.net.URLEncoder;
import com.mysql.jdbc.StringUtils;

@WebServlet("/questionhistory.do")
public class questionhistory extends HttpServlet {

	/****** 資料庫變數 ******/
	String sqlQuery = "";
	ResultSet rs;
	dbBean db = new dbBean();
	int displaycount = 10;  //設置每頁顯示10筆
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		/****** 設定 ******/
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		db.setDataSource("jdbc/ctmpllab");
		db.Connected();
		PrintWriter out = response.getWriter();// Write the response back to the browser
		
		String username = (String)request.getSession().getAttribute("login");
		String type = request.getParameter("type");
		String Id = request.getParameter("Id"); //身分證字號字號
		int count = Integer.parseInt(request.getParameter("count")); //目前頁面顯示位置
		
		String filename ="";
		String category ="";
		
		
		ArrayList<ArrayList<String>> databasedata = new ArrayList<ArrayList<String>>(); //databasedata : [ ArrayList [time_list] , ArrayList [category_list] ]
		ArrayList<String> time_list = new ArrayList<String>(); //填寫時間列表
		ArrayList<String> category_list = new ArrayList<String>(); //問卷類型列表
		lib timeS = new lib();
		databasedata = getdatabase(Id,username);
		time_list = databasedata.get(0); //填寫時間列表
		category_list = databasedata.get(1); //問卷類型列表
		int displaynum = time_list.size(); //最終每頁顯示數量
		
		switch (type) {
		case "getdata":			
			if (time_list.size() > displaycount){
				out.print("nextpage:inline|"); //顯示nextpage
				out.print(displaycount+"|");
			}
			
			if ((count+displaycount) < displaynum)
				displaynum = count + displaycount;
			
			out.print("<tr><td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">填寫時間</td>");
			out.print("<td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">問卷類型</td></tr>");
			
			for (int i=0 ; i<displaynum ; i++){
				filename = timeS.GenTimeStamp(time_list.get(i));
				category = category_list.get(i);
				out.print("<tr><td align="+"\"center\""+">");
				out.print("<a href="+"\"showHistory.jsp?filename="+time_list.get(i)+"&category="+URLEncoder.encode(category)+"&Id="+Id+"\" target="+"\"_new\">");
				out.print(filename+"</a></td>");
				out.print("<td align="+"\"center\""+">"+category+"</td></tr>");	
			}
			
			break;
			
		case "nextpage":
			if ((count+displaycount) >= time_list.size()){
				displaynum = time_list.size();
				out.print("nextpage:none|"); //不顯示nextpage
			}
			else{
				displaynum = count + displaycount;
				out.print("nextpage:inline|"); //顯示nextpage
			}
			out.print("prevouspage:inline|"); //顯示prevouspage
			out.print(displaynum+"|");
			
			out.print("<tr><td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">填寫時間</td>");
			out.print("<td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">問卷類型</td></tr>");
			
			for (int i=count ; i<displaynum ; i++){
				filename = timeS.GenTimeStamp(time_list.get(i));
				category = category_list.get(i);
				out.print("<tr><td align="+"\"center\""+">");
				out.print("<a href="+"\"showHistory.jsp?filename="+time_list.get(i)+"&category="+URLEncoder.encode(category)+"&Id="+Id+"\" target="+"\"_new\">");
				out.print(filename+"</a></td>");
				out.print("<td align="+"\"center\""+">"+category+"</td></tr>");	
			}
			
			break;
			
		case "prevouspage":
			if (count == time_list.size()){
				if ((count % displaycount) == 0)
					displaynum = count - displaycount;
				else
					displaynum = count - (count % displaycount);
			}else{
				displaynum = count - displaycount;
			}
			
			
			out.print("nextpage:inline|"); //顯示nextpage
			if ((displaynum-displaycount) <= 0){
				out.print("prevouspage:none|"); //不顯示prevouspage
			}else{
				out.print("prevouspage:inline|"); //顯示prevouspage
			}		
			out.print(displaynum+"|");
			
			out.print("<tr><td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">填寫時間</td>");
			out.print("<td class="+"\"TEXT_STYLE\""+ "align="+"\"center\">問卷類型</td></tr>");
			
			for (int i=displaynum-displaycount ; i<displaynum ; i++){
				filename = timeS.GenTimeStamp(time_list.get(i));
				category = category_list.get(i);
				out.print("<tr><td align="+"\"center\""+">");
				out.print("<a href="+"\"showHistory.jsp?filename="+time_list.get(i)+"&category="+URLEncoder.encode(category)+"&Id="+Id+"\" target="+"\"_new\">");
				out.print(filename+"</a></td>");
				out.print("<td align="+"\"center\""+">"+category+"</td></tr>");	
			}
			
			break;
		}
		
		out.close();
	}
	
	private ArrayList<ArrayList<String>> getdatabase(String Id,String username){
		ArrayList<ArrayList<String>> temp = new ArrayList<ArrayList<String>>();
		ArrayList<String> time_list = new ArrayList<String>(); //填寫時間列表
		ArrayList<String> category_list = new ArrayList<String>(); //問卷類型列表
		sqlQuery = "select testTime,categoryname from questionTable where identity = '" + Id + "'OR account = '"+Id+"' order by id desc";
		try {
			rs = db.ExcuteQuery(sqlQuery);
			while (rs.next()){
				time_list.add(rs.getString("testTime"));
				category_list.add(rs.getString("categoryname"));
			}
			db.CloseConn();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		temp.add(time_list);
		temp.add(category_list);
		return temp; //temp : [ ArrayList [time_list] , ArrayList [category_list] ]
	}
	
}
